<?php
namespace app\manage\controller;
use think\Config;
use think\Controller;
use app\common\controller\ReturnCode;

class Login extends controller
{
    /**
     * 后台登录
     */
    public function index()
    {
        
        if(request()->isAjax()){
            $postData = input('post.');
            if(!captcha_check($postData['code'])) return self::showJsonReturnCode(1023); // 校对图形验证码
            $res = model('Admin')->checkPassword($postData['phone'],$postData['password']); // 校对账号密码
            if($res == 0){
                return self::showJsonReturnCode(1022); // 用户账号被禁用
            }else if($res == -1){
                return self::showJsonReturnCode(1021); // 用户账号或密码错误
            }else if($res == -2){
                return self::showJsonReturnCode(1023,'账号不存在'); // 账号不存在
            }else{
                $this->RecordSystemLog($res,'Login/index','成功登录');
                $result = model('AdminToken')->setLongLogin($postData['phone'],$res); // 设置永久登录
                if($result){
                    return self::showJsonReturnCode(1001); // 登录成功
                }else{
                    return self::showJsonReturnCode(1009, '登录失败');
                }
            }
        }

        $this->checkLong(); // 判断是否持久登录

        if(session('uid')){
            $this->redirect("index/index");
        }
        return $this->fetch();
    }


    /**
     * 判断是否持久登录
     */
    public function checkLong(){
        $is_long = model('AdminToken')->editToken('checkRemember');
        if($is_long === false){    // 只允许一个客户端登录
            session('uid',null);  // 删除判断登录的凭据
            cookie('think_auth',null);
        }else{
            session("phone",$is_long['phone']);
            session("uid",$is_long['id']);
        }
    }


    /**
     * 退出
     */
    public function logout(){
        $res = model('AdminToken')->editToken('deltoken'); // 退出后删除token
        if($res){
            session('uid',null);
            cookie('think_auth',null);
            $this->redirect('login/index');
        }else{
            $this->error('退出失败');
        }
    }

    static public function showJsonReturnCode($code = '', $msg = ''){
        Config::set(['default_return_type'    => 'json',]);
        return self::showReturnCodeData($code, [], $msg);
    }


    /**
     * 获取返回码数组  有返回值
     * @param string $code
     * @param array $data
     * @param string $msg
     * @return array
     */
    static public function showReturnCodeData($code = '', $data = [], $msg = '')
    {
        $return_data = [
            'code' => '500',
            'msg' => '未定义消息',
            'data' => $code == 1001 ? $data : [],
        ];
        if (empty($code)) return $return_data;
        $return_data['code'] = $code;
        if(!empty($msg)){
            $return_data['msg'] = $msg;
        }else if (isset(ReturnCode::$return_code[$code]) ) {
            $return_data['msg'] = ReturnCode::$return_code[$code];
        }
        return $return_data;
    }


    /**
     * 记录系统日志
     * @param string $behavior
     * @param array $arr
     */
    public function RecordSystemLog($uid,$behavior,$description)
    {
        $data['uid'] = $uid;
        //$data['ip'] = $this->request->ip();
		$data['ip'] = '127.0.0.1';
        $data['behavior'] = $behavior;
        $data['description'] = $description;
        model('SystemLog')->writeSystemLog($data);  // 日志入库
    }

}

